<?php
class Admin_PicController extends My_Controllers_Admin {
	public function indexAction() {
		$id = $this->_getParam ( 'id' );
		if ($id == '') {
			$this->view->action = 'insert';
		} else {
			$this->view->action = 'save';
			$pic = new Pic ( );
			$this->view->row = $row = $pic->find ( $id )->current ();
		}
		
		Zend_Debug::dump ( $row, '', false );
	}
	public function insertAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		
		Zend_Debug::dump ( $_FILES );
		//		exit ();
		if ($_FILES ['pic'] ['name'] != '') {
			$pic_url = time ();
			$ext = pathinfo ( $_FILES ['pic'] ['name'], PATHINFO_EXTENSION );
			$img = & My_Helper_Image::createFromFile ( $_FILES ['pic'] ['tmp_name'], $ext );
			$img->crop ( 430, 200, true, array ('pos' => 'center', 'enabled' => true ) );
			$img->saveAsPng ( "./html/upfile/pic/$pic_url.png" );
			
			$img = & My_Helper_Image::createFromFile ( $_FILES ['pic'] ['tmp_name'], $ext );
			$img->crop ( 160, 160, true, array ('pos' => 'center', 'enabled' => true ) );
			$img->saveAsPng ( "./html/upfile/pic/160-160/$pic_url.png" );
			$Date ['src'] = $pic_url . ".png";
			Zend_Debug::dump ( $Date );
		}
		$pic = new Pic ( );
		$Date ['summary'] = $this->_getParam ( 'summary' );
		$Date ['link'] = $this->_getParam ( 'link' );
		$Date ['order'] = $this->_getParam ( 'order' );
		try {
			$id = $pic->insert ( $Date );
			$this->_redirect ( "/admin/pic/list" );
			echo $id;
		} catch ( PDOException $e ) {
			echo "Exception caught : {$e->getMessage()}\n";
		}
	}
	public function saveAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		$id = $this->_getParam ( 'id' );
		
		$pic = new Pic ( );
		$row = $pic->find ( $id );
		
		if ($_FILES ['pic'] ['name'] != '') {
			$pic_url = $row->current ()->src;
			$ext = pathinfo ( $_FILES ['pic'] ['name'], PATHINFO_EXTENSION );
			$img = & My_Helper_Image::createFromFile ( $_FILES ['pic'] ['tmp_name'], $ext );
			$img->crop ( 430, 200, true, array ('pos' => 'center', 'enabled' => true ) );
			$img->saveAsPng ( "./html/upfile/pic/$pic_url" );
			
			$img = & My_Helper_Image::createFromFile ( $_FILES ['pic'] ['tmp_name'], $ext );
			$img->crop ( 160, 160, true, array ('pos' => 'center', 'enabled' => true ) );
			$img->saveAsPng ( "./html/upfile/pic/160-160/$pic_url" );
		
		}
		
		$row->current ()->summary = $this->_getParam ( 'summary' );
		$row->current ()->link = $this->_getParam ( 'link' );
		$row->current ()->order = $this->_getParam ( 'order' );
		$row->current ()->save ();
		$this->_redirect ( "/admin/pic/list" );
	}
	public function delAction() {
		$front = Zend_Controller_Front::getInstance ();
		$front->setParam ( 'noViewRenderer', true );
		$id = $this->_getParam ( 'id' );
		
		$pic = new Pic ( );
		$row = $pic->find ( $id );
		$row->current ()->delete ();
		$this->_redirect ( "/admin/pic/list" );
	}
	public function listAction() {
		$this->view->setHelperPath ( './application/views/helpers', 'My_View_Helper' );
		
		$pic = new Pic ( );
		$select = $pic->select ();
		$select->order ( 'id DESC' );
		$rows = $this->view->rows = $pic->fetchAll ( $select );
		Zend_Debug::dump ( $rows, '', false );
	}
}
?>